package com.yf.dxs.wo.service.impl;

import com.alibaba.fastjson2.JSON;
import com.yf.dxs.wo.dao.WoGoodsMsgMapper;
import com.yf.dxs.wo.entity.WoGoodsMsgDO;
import com.yf.dxs.wo.redis.RedisMqTemplate;
import com.yf.dxs.wo.req.msg.WoCardMsg;
import com.yf.dxs.wo.req.msg.WoGoodsMsg;
import com.yf.dxs.wo.service.MsgHandler;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class GoodsHandler implements MsgHandler {


    @Resource
    private WoGoodsMsgMapper goodsMsgMapper;

    @Resource
    private RedisMqTemplate redisMqTemplate;

    @Override
    public void handle(String msgId, String msgBody, String event) {
        WoGoodsMsg msg = JSON.parseObject(msgBody, WoGoodsMsg.class);
        WoGoodsMsgDO entity = msg.toEntity();
        entity.setMsgId(msgId);
        log.info("商品变更:{}", entity.getGoodsId());
        int i = goodsMsgMapper.insert(entity);
        if (i == 0) {
            log.info("商品变更失败:{}", entity.getGoodsId());
        } else {
            log.info("商品变更成功:{}", entity.getGoodsId());
            redisMqTemplate.sendMsg(entity);
        }
    }
}
